﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using COFE.Core.Lister;
using System.IO;

namespace COFE.IO.SZ
{
    public class SevenZipStreamProvider : StreamProviderBase<SZEntryInfo>
    {
        #region Constructor
        public SevenZipStreamProvider(IDirectoryLister lister)
            : base(lister)
        {
            //_getStream is not used.
        }

        #endregion

        #region Methods

        public override Stream OpenStream(Core.IInternalFileInfoExA file, FileMode mode, FileAccess access, FileShare share)
        {           
            if (access == FileAccess.Read)
            {
                SZEntryInfo embeddedEntry = getEmbeddedItem(file);
                MemoryStream extractStream = new MemoryStream();
                var extractor = embeddedEntry.RootWrapper.getExtractor();
                if (extractor.ArchiveFileNames.Contains(embeddedEntry.RelativePath))
                {
                    extractor.ExtractFile(embeddedEntry.RelativePath, extractStream);
                    extractStream.Seek(0, SeekOrigin.Begin);
                    return extractStream;
                }
            }

            return new SevenZipFileStream(file, mode, access);


        }

        #endregion

    }
}